package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProcessTargetMapper;
import com.indusfo.spc.pojo.ProcessTarget;
import com.indusfo.spc.service.ProcessTargetService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工艺目标业务层
 *  
 * @author xuz
 * @date 2018/12/6 5:13 PM
 */
@Service
public class ProcessTargetServiceImpl implements ProcessTargetService {
    // 声明日志的静态变量
    private static final Logger logger = LoggerFactory.getLogger(ProcessTargetServiceImpl.class);

    @Resource
    private ProcessTargetMapper processTargetMapper;

    /**
     * 分页查询 pagesize pageindex
     * 模糊查询 dim
     * ID查询  Pid
     * 返回总数 count
     * @param processTarget
     * @return
     */
    @Override
    public JSONObject queryProcessTarget(ProcessTarget processTarget) {
        try {
            //获取页面的数据个数
            Integer pagesize = processTarget.getPagesize();
            //获取查询的页数
            Integer pageindex = processTarget.getPageindex();
            if(pagesize!=null && pageindex !=null){
                processTarget.setIncept(pagesize*(pageindex-1));
            }
            List<ProcessTarget> listProcessTarget=processTargetMapper.queryProcessTarget(processTarget);
            if(listProcessTarget.isEmpty()){
                return  JSONObject.oK("没有查询到相关消息",listProcessTarget,0);
            }
            //如果页数和页面数量都不为空的情况下进行计数

            Integer count= processTargetMapper.countProcessTarget(processTarget);
            return JSONObject.oK("查询成功",listProcessTarget,count);
        } catch (GlobalException e) {
            // 捕获异常,打印并返回数
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 非空判断 长度验证   checkParam
     * 查重判断 checkParamsNotRepeat   问题这个不需要查重吧?
     * 默认值赋值 setDataState(1)
     * @param processTarget
     * @return
     */
    @Override
    public JSONObject insertProcessTarget(ProcessTarget processTarget) {
        /**
         * 这里还要判断哪些不为空
         */
        try{
            if(processTarget.getlProTarget()!=null){
                throw new ModifyFailedException("新增是Id不能存在");
            }
            if(processTarget.getlDataState()==null){
                processTarget.setlDataState(1);
            }
            checkParam(processTarget);

            processTarget.setlDataState(1);
            Integer row=processTargetMapper.insertProcessTarget(processTarget);
            if(row==0){
                throw new ModifyFailedException("新增不良现象失败");
            }
            return JSONObject.oK("新增成功");
        }catch(GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /*多数据删除 Long[] bldcIds
    级联删除 delteBldcByBldcTypeId*/
    @Override
    public JSONObject deleteProcessTarget(List<Integer> processTargetIds, Integer IDataState) {
        try{
            //判断传入的参数是否为null
            if(IDataState == null) {
                //为null抛出异常
                throw new ParamsErrorException("数据状态不能为空");
            }else if(IDataState != 1 && IDataState != 2 && IDataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if(processTargetIds == null) {
                throw new ParamsErrorException("请选择设备资料");
            }
            //调用删除的接口
            int row = processTargetMapper.deleteProcessTarget(processTargetIds,IDataState);
            //声明msg字符串，用来存抛出的异常
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (IDataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                        break;
                }
            } else {
                switch (IDataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                        break;
                }
            }
            return JSONObject.oK(msg);
        }catch(GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 非空判断 checkParam
     * 查重判断 checkParamsNotRepeat
     * 默认值赋值 setDataState(1)
     * @param processTarget
     * @return
     */
    @Override
    public JSONObject updateProcessTarget(ProcessTarget processTarget) {
        try {
            //判断传入的参数是否为null
            if(processTarget.getlProTarget()==null) {
                //为null则抛出异常
                throw new ParamsErrorException("修改不良现象id不能为空");
            }
            if(processTarget.getlDataState()==null){
                processTarget.setlDataState(1);
            }
            checkParam(processTarget);

            //调用修改的接口
            int row = processTargetMapper.updateProcessTarget(processTarget);
            if(row == 0) {
                throw new ModifyFailedException("数据修改失败");
            }
            return JSONObject.oK("数据修改成功");
        }catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }
    public void checkParam(ProcessTarget processTarget){
        //规格上限
        String vcUpperLimit = processTarget.getVcUpperLimit();
        //规格下限
        String vcLowerLimit = processTarget.getVcLowerLimit();
        //中心值
        String vcCenter = processTarget.getVcCenter();
        //内控上限
        String vcUpperIn = processTarget.getVcUpperIn();
        //内控下限
        String vcLowerIn = processTarget.getVcLowerIn();
        //工艺流程id
        Integer lProFlow = processTarget.getlProFlow();
        //分组
        Integer lDeteGroup = processTarget.getlDeteGroup();
        //每组检测数
        Integer lCounts = processTarget.getlCounts();
        //获取工艺目标id
        Integer lProTarget = processTarget.getlProTarget();
        if (StringUtils.isEmpty(vcUpperLimit)) {
            throw new ParamsErrorException("请填写规格上限");
        }
        if (StringUtils.isEmpty(vcLowerLimit)) {
            throw new ParamsErrorException("请填写规格下限");
        }
        if (StringUtils.isEmpty(vcCenter)) {
            throw new ParamsErrorException("请填写规格中心值");
        }
        if (vcUpperLimit.getBytes().length > 50) {
            throw new ParamsErrorException("规格上限超过规定长度");
        }
        if (vcLowerLimit.getBytes().length > 50) {
            throw new ParamsErrorException("规格下限超过规定长度");
        }
        if (vcCenter.getBytes().length > 50) {
            throw new ParamsErrorException("规格中心值超过规定长度");
        }
        //上下限数值判断
        if (Double.parseDouble(vcUpperLimit) <= Double.parseDouble(vcLowerLimit)) {
            throw new ParamsErrorException("规格上限值小于规格下限值");
        }
        if (Double.parseDouble(vcCenter) < Double.parseDouble(vcLowerLimit)) {
            throw new ParamsErrorException("规格中心值小于规格下限值");
        }
        if (Double.parseDouble(vcCenter) > Double.parseDouble(vcUpperLimit)) {
            throw new ParamsErrorException("规格中心值大于规格上限值");
        }
        // 名称不能重复
        Integer Counts = processTargetMapper.checkProcessTarget(processTarget);
        if (Counts !=0) {
            throw new ParamsErrorException("该控制项目已存在");
        }

    }



}
